//
//  XGHomeViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/2/3.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import JXSegmentedView

class XGHomeViewController: XGBaseListContainerViewController
{
    // MARK: - 控制器生命周期方法
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        navigationItem.titleView = XGHomeNavBarView()
    }
    
    override func loadCategories(completion: @escaping ([String]) -> Void)
    {
        viewModel.fetchNewsCategories { (isSuccess) in
            if isSuccess {
                let titles = self.viewModel.newsCategories.compactMap({ return $0.name })
                completion(titles)
            }
        }
    }
    
    // MARK: - 私有属性
    
    /// 视图模型
    private let viewModel = XGHomeViewModel()
}

// MARK: - JXSegmentedListContainerViewDataSource

extension XGHomeViewController
{
    override func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int
    {
        return viewModel.newsCategories.count
    }
    
    override func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate
    {
        let newsListTableViewController = XGNewsListTableViewController(newsCategory: viewModel.newsCategories[index])
        newsListTableViewController.newsDelegate = self
        return newsListTableViewController
    }
}

// MARK: - XGNewsListTableViewControllerDelegate

extension XGHomeViewController: XGNewsListTableViewControllerDelegate
{
    func newsDidSelected(newsViewModel: XGNewsViewModel)
    {
        let newsDetailViewController = XGNewsDetailViewController(newsViewModel: newsViewModel)
        navigationController?.pushViewController(newsDetailViewController, animated: true)
    }
}

